#include "cJSON/cJSON.h"
#include "log/log.h"
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    //1、 创建Json对象
    char *name = "zhangsan";
    int age = 18;
    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "name", name);
    cJSON_AddNumberToObject(root, "age", age);
    /* 无格式输出 */
    // char *json_str = cJSON_PrintUnformatted(root);
    /* 有格式输出 */
    char *json_str = cJSON_Print(root);
    log_debug("json_str: %s", json_str);

    //2、 解析Json对象
    cJSON *json = cJSON_Parse(json_str);
    if (json == NULL){
        log_error("json parse failed");
        return -1;
    }
    cJSON *name_json = cJSON_GetObjectItem(json, "name");
    cJSON *age_json = cJSON_GetObjectItem(json, "age");
    char *name_str = name_json->valuestring;
    int age_num = age_json->valueint;
    log_debug("name: %s, age: %d", name_str, age_num);

    //3、 释放Json对象
    cJSON_Delete(json);
    cJSON_Delete(root);
    free(json_str);

    return 0;
}
